package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import org.springframework.web.bind.annotation.*;

/**
 * @author LiangTianQi
 * @describe
 * @date 2020/11/9 10:12
 */
@RestController
@RequestMapping("member")
public class MemberController {

    @Reference
    private MemberService memberService;

    @PostMapping("/getByConditions")
    public Result getByConditions(@RequestBody QueryPageBean queryPageBean) {
        try {
            PageResult pageResult = memberService.getByConditions(queryPageBean);
            return new Result(true,"查询会员成功",pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,"查询会员失败");
        }
    }

    @PostMapping("/add")
    public Result add(@RequestBody Member member){
        //调用service传入参数
        try {
            memberService.add(member);
            return new Result(true,MessageConstant.ADD_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_CHECKGROUP_FAIL);
        }
    }

    @GetMapping("/get")
    public Result get(Integer id) {
        Member member = null;
        try {
            member = memberService.get(id);
            return new Result(true,"获取会员成功",member);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"获取会员失败");
        }
    }

    /**
     * 更新检查组，还有中间表的关系
     * @param member
     * @return
     */
    @PutMapping("/update")
    public Result update(@RequestBody Member member){
        //调用service传入参数
        try {
            memberService.update(member);
            return new Result(true,"会员更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"会员更新失败");
        }
    }

    /**
     * 删除检查组，并且删除中间表信息
     * @param id
     * @return
     */
    @GetMapping("/delete")
    public Result delete(Integer id) {

        try {
            memberService.delete(id);
            return new Result(true,"会员删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"会员删除失败");
        }
    }

}
